import Core.Basic
from Core.package import *
from .application_ui import Ui_Form





class MyWidget(QWidget):
    
    
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.SetVar()
        self.SetUi()
        self.SetBind()
        self.SetHook()
        self.SetThread()
        self.SetQThreadPool()

    def SetVar(self):
        self.threadObject = QThread()        
        self.ThreadPool = QThreadPool()
        self.timer = QTimer()
        self.ThreadPool.setMaxThreadCount(10)
        self.ui.tableWidget.setHorizontalHeaderLabels(["Handle","Title","Url"])
        self.ui.tableWidget.horizontalHeader().setStretchLastSection(True)
        self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)

    def SetUi(self):
        self.setWindowTitle("一键抄款工具!")
        self.setFixedWidth(self.width())
        self.setFixedHeight(self.height())
        # self.setWindowFlags(self.windowFlags() | Qt.)
        
        
    def SetBind(self):
        self.ui.pushButton_check_browser_status.clicked.connect(self.on_pushButton_check_browser_status_clicked)
        self.ui.pushButton_close_browser_process.clicked.connect(self.on_pushButton_close_browser_process)
        self.ui.pushButton_start_browser_not_plugins.clicked.connect(self.on_pushButton_start_browser_not_plugins)
        self.ui.pushButton_start_browser_has_plugins.clicked.connect(self.on_pushButton_start_browser_has_plugins)
        

        # 
        self.ui.pushButton_init_selenium_frame.clicked.connect(self.on_pushButton_init_selenium_frame)
        self.ui.pushButton_selenium_switch_to_active.clicked.connect(self.on_pushButton_selenium_switch_to_active)
        self.ui.pushButton_get_all_handles.clicked.connect(self.on_pushButton_get_all_handles)
    

        # 
        self.ui.pushButton_open_doudian.clicked.connect(self.on_pushButton_open_doudian)
        self.ui.pushButton_open_qianniu.clicked.connect(self.on_pushButton_open_qianniu)
        self.ui.pushButton_open_pdd.clicked.connect(self.on_pushButton_open_pdd)
        self.ui.pushButton_open_soukuan.clicked.connect(self.on_pushButton_open_soukuan)
        self.ui.pushButton_open_17wang.clicked.connect(self.on_pushButton_open_17wang)
        self.ui.pushButton_open_taobao.clicked.connect(self.on_pushButton_open_taobao)
        self.ui.pushButton_open_1688.clicked.connect(self.on_pushButton_open_1688)
        
    
        # 采集功能绑定
        self.ui.pushButton_get_data_taobao.clicked.connect(self.on_pushButton_get_data_taobao)
        self.ui.pushButton_get_data_xiaohongshu.clicked.connect(self.on_pushButton_get_data_xiaohongshu)
        self.ui.pushButton_get_data_douyin.clicked.connect(self.on_pushButton_get_data_douyin)
        self.ui.pushButton_get_data_douyin_zhibo.clicked.connect(self.onpushButton_get_data_douyin_zhibo)
        self.ui.pushButton_get_data_soukuan.clicked.connect(self.on_pushButton_get_data_soukuan)
        self.ui.pushButton_get_data_17wang.clicked.connect(self.on_pushButton_get_data_17wang)

        # 商品配置信息相关功能绑定
        self.ui.pushButton_save_product_data_file.clicked.connect(self.on_pushButton_save_product_data_file)
        self.ui.pushButton_open_data_directory.clicked.connect(self.on_pushButton_open_data_directory)
        self.ui.pushButton_scanf_configfile.clicked.connect(self.on_pushButton_scanf_configfile)
        self.ui.pushButton_fast_save.clicked.connect(self.on_pushButton_fast_save)
        self.ui.pushButton_clear_getdataed.clicked.connect(lambda : self.ui.textEdit_get_datas.clear())


        # 下载功能相关绑定
        self.ui.pushButton_download_product_data_file.clicked.connect(self.on_pushButton_download_product_data_file)
        self.ui.pushButton_all_download_product_data_file.clicked.connect(self.on_pushButton_all_download_product_data_file)
        self.ui.pushButton_open_download_directory.clicked.connect(self.on_pushButton_open_download_directory)
        
        
        # 上传配置文件相关功能绑定
        self.ui.pushButton_scanf_localtion_upload_config_file.clicked.connect(self.on_pushButton_scanf_localtion_upload_config_file)
        self.ui.comboBox_localtion_upload_config_file.currentIndexChanged.connect(self.on_comboBox_localtion_upload_config_file_currentIndexChanged)
        self.ui.pushButton_update_localtion_upload_config.clicked.connect(self.on_pushButton_update_localtion_upload_config)
        self.ui.pushButton_saveas_localtion_upload_config.clicked.connect(self.on_pushButton_saveas_localtion_upload_config) 


        # 写入 商品自动化配置文件的相关功能
        self.ui.pushButton_scanf_needcopy_directory.clicked.connect(self.on_pushButton_scanf_needcopy_directory)
        self.ui.pushButton_copy_to_directory.clicked.connect(self.on_pushButton_copy_to_directory)
        self.ui.pushButton_copy_to_scriptfile.clicked.connect(self.on_pushButton_copy_to_scriptfile)
        self.ui.pushButton_copy_to_scriptdirectory.clicked.connect(self.on_pushButton_copy_to_scriptdirectory)

        # 启动脚本
        self.ui.pushButton_scanf_upload_product.clicked.connect(self.on_pushButton_scanf_upload_product)


        
    def SetHook(self):
        pass

    def SetThread(self):
        pass

    def SetQThreadPool(self):
        pass


    def closeEvent(self, event) -> None:
        # 重写了这个主窗口的关闭程序.这样子在主线程关闭的时候,也会关闭子线程.
        # 停止线程
        # 终止线程池中的所有线程
        self.threadObject.quit()
        self.threadObject.wait()
        self.ThreadPool.clear()
        self.deleteLater()
        print("主线程关闭")
        event.accept()





    
    # Event Binds
    def on_pushButton_check_browser_status_clicked(self):
        from Staff.Staff_Monitor_Browser_Debug_Status.Staff_Class import Staff_Browser_Check_Status
        staff = Staff_Browser_Check_Status(self)
        staff.Start_Task.emit()

        
    def on_pushButton_close_browser_process(self):
        from Staff.Staff_Browser_Close_Browser.Staff_Class  import Staff_Browser_Close_Browser
        staff = Staff_Browser_Close_Browser(self)
        staff.Start_Task.emit()


    def on_pushButton_start_browser_not_plugins(self):
        from Staff.Staff_Browser_Start_Browser_Not_Plugins.Staff_Class import Staff_Browser_Start_Browser_Not_Plugins
        staff = Staff_Browser_Start_Browser_Not_Plugins(self)
        staff.Start_Task.emit()

    
    def on_pushButton_start_browser_has_plugins(self):
        from Staff.Staff_Browser_Start_Browser_Has_Plugins.Staff_Class import Staff_Browser_Start_Browser_Has_Plugins
        staff = Staff_Browser_Start_Browser_Has_Plugins(self)
        staff.Start_Task.emit()


    def on_pushButton_init_selenium_frame(self):
        from Staff.Staff_Init_Selenium_Frame.Staff_Class import Staff_Init_Selenium_Frame
        staff = Staff_Init_Selenium_Frame(self)
        staff.Start_Task.emit()


    def on_pushButton_selenium_switch_to_active(self):
        from Staff.Staff_Switch_Active_Handle.Staff_Class import Staff_Switch_Active_Handle
        staff = Staff_Switch_Active_Handle(self)
        staff.Start_Task.emit()


    def on_pushButton_open_doudian(self):
        from Staff.Staff_Open_DouDian.Staff_Class import Staff_Open_DouDian
        staff = Staff_Open_DouDian(self)
        staff.Start_Task.emit()


    def on_pushButton_open_qianniu(self):
        from Staff.Staff_Open_QianNiu.Staff_Class import Staff_Open_QianNiu
        staff = Staff_Open_QianNiu(self)
        staff.Start_Task.emit()


    def on_pushButton_open_pdd(self):
        from Staff.Staff_Open_Pdd.Staff_Class import Staff_Open_Pdd
        staff = Staff_Open_Pdd(self)
        staff.Start_Task.emit()


    def on_pushButton_open_soukuan(self):
        from Staff.Staff_Open_SouKuan.Staff_Class import Staff_Open_SouKuan
        staff = Staff_Open_SouKuan(self)
        staff.Start_Task.emit()


    def on_pushButton_open_17wang(self):
        from Staff.Staff_Open_17Wang.Staff_Class import Staff_Open_17Wang
        staff = Staff_Open_17Wang(self)
        staff.Start_Task.emit()


    def on_pushButton_open_taobao(self):
        from Staff.Staff_Open_TaoBaoWang.Staff_Class import Staff_Open_TaoBaoWang
        staff = Staff_Open_TaoBaoWang(self)
        staff.Start_Task.emit()


    def on_pushButton_open_1688(self):
        from Staff.Staff_Open_1688.Staff_Class import Staff_Open_1688
        staff = Staff_Open_1688(self)
        staff.Start_Task.emit()

    def on_pushButton_get_all_handles(self):
        from Staff.Staff_Get_All_Handles.Staff_Class import Staff_Get_All_Handles
        staff = Staff_Get_All_Handles(self)
        staff.Start_Task.emit()


    def on_pushButton_get_data_taobao(self):
        from Staff.Staff_Get_Data_TaoBao.Staff_Class import Staff_Get_Data_TaoBao
        staff = Staff_Get_Data_TaoBao(self)
        staff.Start_Task.emit()


    def on_pushButton_get_data_xiaohongshu(self):
        from Staff.Staff_Get_Data_XiaoHongShu.Staff_Class import Staff_Get_Data_XiaoHongShu
        staff = Staff_Get_Data_XiaoHongShu(self)
        staff.Start_Task.emit()


    def on_pushButton_get_data_douyin(self):
        from Staff.Staff_Get_Data_DouYin.Staff_Class import Staff_Get_Data_DouYin
        staff = Staff_Get_Data_DouYin(self)
        staff.Start_Task.emit()


    def onpushButton_get_data_douyin_zhibo(self):
        from Staff.Staff_Get_Data_DouYin_ZhiBo.Staff_Class import Staff_Get_Data_DouYin_ZhiBo
        staff = Staff_Get_Data_DouYin_ZhiBo(self)
        staff.Start_Task.emit()


    def on_pushButton_get_data_soukuan(self):
        from Staff.Staff_Get_Data_SouKuan.Staff_Class import Staff_Get_Data_SouKuan
        staff = Staff_Get_Data_SouKuan(self)
        staff.Start_Task.emit()


    def on_pushButton_get_data_17wang(self):
        from Staff.Staff_Get_Data_17Wang.Staff_Class import Staff_Get_Data_17Wang
        staff = Staff_Get_Data_17Wang(self)
        staff.Start_Task.emit()

    def on_pushButton_save_product_data_file(self):
        from Staff.Staff_Save_Product_Data.Staff_Class import Staff_Save_Product_Data
        staff = Staff_Save_Product_Data(self)
        staff.Start_Task.emit()


    def on_pushButton_open_data_directory(self):
        self.ui.pushButton_open_data_directory.setEnabled(False)
        if pathlib.Path("./Data/Product_Collection_ConfigFile").absolute().resolve().exists() == False:
            pathlib.Path("./Data/Product_Collection_ConfigFile").absolute().resolve().mkdir(parents=True, exist_ok=True)
            os.startfile(filepath=pathlib.Path("./Data/Product_Collection_ConfigFile").absolute().resolve())
        else:
            os.startfile(filepath=pathlib.Path("./Data/Product_Collection_ConfigFile").absolute().resolve())
        self.ui.pushButton_open_data_directory.setEnabled(True)


    def on_pushButton_fast_save(self):
        self.ui.pushButton_fast_save.setEnabled(False)
        if pathlib.Path("./Data/Product_Collection_ConfigFile").absolute().resolve().exists() == False:
            pathlib.Path("./Data/Product_Collection_ConfigFile").absolute().resolve().mkdir(parents=True, exist_ok=True)
        
        SaveTime = datetime.datetime.now().strftime("%Y年%m月%d日%H时%M分%S秒")
        with open(pathlib.Path(f"./Data/Product_Collection_ConfigFile/{SaveTime}.json"), mode="w", encoding="utf-8") as f:
            f.write(self.ui.textEdit_get_datas.toPlainText())
        QMessageBox.information(self, "提示", "保存成功！")
        self.ui.pushButton_fast_save.setEnabled(True)


    def on_pushButton_scanf_configfile(self):
        from Staff.Staff_Scanf_ConfigData.Staff_Class import Staff_Scanf_ConfigData
        staff = Staff_Scanf_ConfigData(self)
        staff.Start_Task.emit()


    def on_pushButton_download_product_data_file(self):
        from Staff.Staff_DownloadFile.Staff_Class import Staff_DownloadFile
        staff = Staff_DownloadFile(self)
        staff.Start_Task.emit()
        

    def on_pushButton_all_download_product_data_file(self):
        from Staff.Staff_DownloadFile_All.Staff_Class import Staff_DownloadFile_All
        staff = Staff_DownloadFile_All(self)
        staff.Start_Task.emit()


    def on_pushButton_open_download_directory(self):
        self.ui.pushButton_open_download_directory.setEnabled(False)
        if pathlib.Path("./Data/Product_Download_File").absolute().resolve().exists() == False:
            pathlib.Path("./Data/Product_Download_File").absolute().resolve().mkdir(parents=True, exist_ok=True)
            os.startfile(filepath=pathlib.Path("./Data/Product_Download_File").absolute().resolve())
        else:
            os.startfile(filepath=pathlib.Path("./Data/Product_Download_File").absolute().resolve())
        self.ui.pushButton_open_download_directory.setEnabled(True)


    def on_pushButton_scanf_localtion_upload_config_file(self):
        from Staff.Staff_Scanf_Upload_ConfigData.Staff_Class import Staff_Scanf_Upload_ConfigData
        staff = Staff_Scanf_Upload_ConfigData(self)
        staff.Start_Task.emit()


    def on_comboBox_localtion_upload_config_file_currentIndexChanged(self):
        self.ui.textEdit_localtion_upload_configfile.clear()
        upload_config_path = pathlib.Path("./Data/Product_Upload_Config_File").joinpath(self.ui.comboBox_localtion_upload_config_file.currentText()).absolute().resolve().__str__()
        self.ui.textEdit_localtion_upload_configfile.setText(open(file=upload_config_path,mode="r",encoding="utf-8").read())
        
    def on_pushButton_update_localtion_upload_config(self):
        from Staff.Staff_Update_Upload_Config_File.Staff_Class import Staff_Update_Upload_Config_File
        staff = Staff_Update_Upload_Config_File(self)
        staff.Start_Task.emit()


    def on_pushButton_saveas_localtion_upload_config(self):
        from Staff.Staff_Saveas_Upload_Config_File.Staff_Class import Staff_Saveas_Upload_Config_File
        staff = Staff_Saveas_Upload_Config_File(self)
        staff.Start_Task.emit()


    def on_pushButton_scanf_needcopy_directory(self):
        from Staff.Staff_Scanf_NeedCopy_Directory.Staff_Class import Staff_Scanf_NeedCopy_Directory
        staff = Staff_Scanf_NeedCopy_Directory(self)
        staff.Start_Task.emit()


    def on_pushButton_copy_to_directory(self):
        from Staff.Staff_Copy_To_Directory.Staff_Class import Staff_Copy_To_Directory
        staff = Staff_Copy_To_Directory(self)
        staff.Start_Task.emit()


    def on_pushButton_copy_to_scriptfile(self):
        from Staff.Staff_Copy_NeedScript.Staff_Class import Staff_Copy_NeedScript
        staff = Staff_Copy_NeedScript(self)
        staff.Start_Task.emit()


    def on_pushButton_copy_to_scriptdirectory(self):
        from Staff.Staff_Copy_NeedDirectory.Staff_Class import Staff_Copy_NeedDirectory
        staff = Staff_Copy_NeedDirectory(self)
        staff.Start_Task.emit()

    def on_pushButton_scanf_upload_product(self):
        from Staff.Staff_Scanf_Upload_Product_Script.Staff_Class import Staff_Scanf_Upload_Product_Script
        staff = Staff_Scanf_Upload_Product_Script(self)
        staff.Start_Task.emit()


